package com.example.weather

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemClickListener
import android.widget.ArrayAdapter
import android.widget.EditText
import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity

/**
 * @author Y7993
 * MsgModelActivity类用于展示消息列表并处理用户选择的消息。
 */
class MsgModelActivity : AppCompatActivity() {
    var lv: ListView? = null
    var msg: Array<String?> =
        arrayOf<String?>("今日寒冷，多穿衣服！", "今日有暴雨，不宜出门", "今日有大风，少出门")
    var searchEt: EditText? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_msg_model)
        initview()
    }

    /**
     * 初始化视图，包括列表视图和编辑文本框，并设置适配器和点击监听器。
     */
    fun initview() {
        lv = findViewById<ListView>(R.id.model_lv)
        searchEt = findViewById<EditText>(R.id.model_et)
        val adapter = ArrayAdapter<String?>(this, R.layout.item_model, msg)
        lv!!.adapter = adapter
        lv!!.onItemClickListener =
            OnItemClickListener { adapterView: AdapterView<*>?, view: View?, pos: Int, id: Long ->
                val intent = getIntent()
                val message = intent.getStringExtra("msg") + "\n" + msg[pos]
                searchEt!!.setText(message)
                val inres = Intent()
                inres.putExtra("msg", message)
                setResult(1, inres)
                finish() //关闭当前页面
            }
        //System.out.println(ans);
    }
}
